# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Dec 13, 2012
@author: Team Death Star
'''

from backend.backend import Backend;
from workflow.task import Task;
from workflow.result import Result;
from workflow.flowData import Status;
from unique import UNIQUE;

class ConstructionBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "construction"+UNIQUE, dashboard=True);

    def wire(self):
        self.register_result_listener("ClientHandler", "ProjectRequest", self.customer_input_complete)
        self.register_result_listener("Architect", "CreateBlueprint", self.create_blueprint_complete)
        #self.register_result_listener("Customer", "ReviewBlueprintByCustomer", self.review_blueprint_by_customer_complete)
        self.register_result_listener("Architect", "SendBlueprintToGovt", self.send_blueprint_to_govt_complete)
        self.register_result_listener("BuildingDept", "ReviewBlueprintByGovt", self.review_blueprint_by_govt_complete)
        self.register_result_listener("Contractor", "ProjectDetails", self.contractor_project_details_complete)
        self.register_result_listener("Contractor", "ManageSubs", self.contractor_manage_subs_complete)        
        self.register_result_listener("Contractor", "FinalizeProject", self.contractor_finalize_project_complete)        
        self.register_result_listener("Subcontractor", "ExecuteSubcontract", self.subcontractor_execute_contract_complete)
      
    def customer_input_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Architect", "CreateBlueprint");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    '''def create_blueprint_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Customer", "ReviewBlueprintByCustomer");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);'''
    
    def create_blueprint_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Architect", "SendBlueprintToGovt");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);
            
    '''def review_blueprint_by_customer_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Architect", "SendBlueprintToGovt");
            #task.set_field('prescriptionnumber', 1);
            self.workflow.add(task);
            #copyFields =['FirstName','LastName','Birthday','InsuranceCompany','CoPay'];
            #task = Task.construct_from_result(result, "Billing", "BillInsurance", copy=copyFields);
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);'''

    def send_blueprint_to_govt_complete(self, results):
        for result in results:
            #if result.data['isprescriptionneeded'] == "TRUE":
                #Add task to write the needed prescrption
            task = Task.construct_from_result(result, "BuildingDept", "ReviewBlueprintByGovt");
                #self.workflow.add(task);
                #Add task to ask if we need another prescription
                #task = Task.construct_from_result(result, "Physician", "IsPrescriptionNeeded");
                #task.set_field('prescriptionnumber',result.get_int_field('prescriptionnumber')+1);
                #task.remove_field('isprescriptionneeded');
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def review_blueprint_by_govt_complete(self, results):
        for result in results:
            #copyFields =['FirstName','LastName','Birthday','DrugName','Dose','Frequency','Refills','IsGenericAcceptable', 'prescriptionnumber'];
            #task = Task.construct_from_result(result, "Contractor", "ExecuteContract", copy=copyFields);
            task = Task.construct_from_result(result, "Contractor", "ProjectDetails");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def contractor_project_details_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Contractor", "ManageSubs");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def contractor_manage_subs_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Contractor", "FinalizeProject");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def contractor_finalize_project_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Subcontractor", "ExecuteSubcontract");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def subcontractor_execute_contract_complete(self, results):
        for result in results:
            self.workflow.update_status(result, Status.COMPLETE);
             

if __name__ == '__main__':
    backend = ConstructionBackend();